
package com.popo.android.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;
import android.widget.Toast;

import com.popo.android.R;
import com.popo.android.activity.BaseActivity;

import java.io.FileDescriptor;

public class LocalBoundService extends Service {

    private IBinder myIBinder = new MyIBinder();
    NotificationManager notificationService =null;
    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("LocalBoundService.onbind ");
        showNotification("onBind");
        return myIBinder;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        notificationService=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        System.out.println("LocalBoundService onCreate");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        System.out.println("LocalBoundService onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        System.out.println("LocalBoundService onStart");
        super.onStart(intent, startId);
    }
    
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        System.out.println("LocalBoundService is destroy");
        notificationService.cancel(0);
        super.onDestroy();
    }

    public class MyIBinder extends Binder {

        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
                throws RemoteException {
            // TODO Auto-generated method stub
            return super.onTransact(code, data, reply, flags);
        }

        public Service getService() {
            return LocalBoundService.this;
        }
        
        public void stop(){
            LocalBoundService.this.stopSelf();
        }
    }

    private void showNotification(String txt) {
        
        Notification notification = new Notification(R.drawable.icon,txt,System.currentTimeMillis());
       
        PendingIntent p = PendingIntent.getActivity(this, 0, new Intent(this, BaseActivity.class),
                PendingIntent.FLAG_CANCEL_CURRENT);
        notification.setLatestEventInfo(this, "Hello MeMi", txt, p);
        System.out.println("notification "+txt);
        notificationService.notify(0, notification);
    }

}
